//
//  RaiseViewController.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/2.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "RaiseViewController.h"
#import "RecordDetailVC.h"
#import "InvestListModel.h"
@interface RaiseViewController ()
{
    
    NSInteger _pageNo;
}
@property (nonatomic, retain)InvestListModel *model;
@property (nonatomic, retain)NSMutableArray *totalArr;
@end

@implementation RaiseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = Normal_BackColor;
    self.tableView.rowHeight = 160;
    _pageNo = 1;
    [self.view showHUDLoading];
    [self statNetworking];
    [self startRefresh];
    self.tableView.mj_footer.hidden = YES;

}
#pragma mark- 刷新
- (void)startRefresh
{
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _pageNo = 1;
        [self statNetworking];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self statNetworking];
    }];
    [self.tableView.mj_header setAutomaticallyChangeAlpha:YES];
    [self.tableView.mj_footer setAutomaticallyChangeAlpha:YES];

}
#pragma tableviewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _totalArr.count;
}
#pragma tableviewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recordCellID"];
    
    UILabel *titleLab = [cell viewWithTag:1001];
    UILabel *amountLab = [cell viewWithTag:1002];
    UILabel *tzqxLab = [cell viewWithTag:1003];
    UILabel *ygsyLab = [cell viewWithTag:1004];
    UILabel *addTimeLab = [cell viewWithTag:1005];
    UILabel *endTimeLab = [cell viewWithTag:1006];
    
    UILabel *totalAmountLabel = [cell viewWithTag:11000];//投资金额
    UILabel *limitTimelabel = [cell viewWithTag:11002];//投资期限
    UILabel *forecastEarningLabel = [cell viewWithTag:11001];//预估总收益

    InvestListModel *model = _totalArr[indexPath.row];
    amountLab.text = model.amount;
    tzqxLab.text = model.tzqx;
    ygsyLab.text = model.ygsy;
    titleLab.text = model.title;
    totalAmountLabel.text = model.profitLabel;
    limitTimelabel.text = model.plstimeLimitLabel;
    forecastEarningLabel.text = model.profitLabel;
    
    
    
    if ([_orderStatus isEqualToString:@"0"]) {
        endTimeLab.text = [NSString stringWithFormat:@"%@:%@",model.endTimeLabel,model.endTime];
        addTimeLab.text = [NSString stringWithFormat:@"%@:%@",model.addTimeLabel,model.addTime];

    }else if ([_orderStatus isEqualToString:@"1"]){
        endTimeLab.text = [NSString stringWithFormat:@"%@:%@",model.endTimeLabel,model.endTime];
        addTimeLab.text = [NSString stringWithFormat:@"%@:%@",model.addTimeLabel,model.addTime];

    }else{
        endTimeLab.text = [NSString stringWithFormat:@"%@:%@",model.endTimeLabel,model.endTime];
        addTimeLab.text = [NSString stringWithFormat:@"%@:%@",model.addTimeLabel,model.addTime];

    }
   
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    RecordDetailVC *detail = [[UIStoryboard storyboardWithName:@"Profile" bundle:nil] instantiateViewControllerWithIdentifier:@"RecordDetailVCID"];
    detail.status = _orderStatus;
    detail.model = _totalArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES withHideBottomBarWhenPush:YES pop:YES];
    
}
- (void)statNetworking
{

    [HYCNetService myWalletInvestDetailListWithDict:@{@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],@"pageSize":@"10",@"orderStatus":_orderStatus} onSuccess:^(NSDictionary *dictResult) {
        //
        NSLog(@"==%@",dictResult);
        [self.view hideHUDLoading:0];
        NSArray *lists = dictResult[@"investList"];
        if (_pageNo==1) {
            [_totalArr removeAllObjects];
            [self.tableView.mj_header endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshing];

        }
        _pageNo++;
        self.tableView.mj_footer.hidden = lists.count<10;
        [self.totalArr addObjectsFromArray:[InvestListModel mj_objectArrayWithKeyValuesArray:lists]];
        [self.tableView reloadData];
    } andFailed:^(NSInteger code, NSString *errorMsg) {
        //

        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView showHUDFailTips:errorMsg hideDelay:1.5];

    }];
}
- (void)setOrderStatus:(NSString *)orderStatus
{
    _orderStatus = orderStatus;
}
/**
 懒加载
 */
- (NSArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}
@end
